package cn.edu.zjgsu.ito.controller;

import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import cn.edu.zjgsu.ito.model.AdminCourseManageModel;
import cn.edu.zjgsu.ito.service.AdminCourseManageService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Record;
import com.mysql.jdbc.Field;

/**
 * @author norman
 * 管理员新增课程
 */
@Before(AdminControllerInterceptor.class)
 public class AdminCourseManageController extends Controller{
	private String majorName;
	private String majorNo;
	private String term;
	private String eduStartSeason;
	private String classroomCheck;
	private String teacherCheck;
	private String studentCheck;
	private String message="";
	private String courseName;
	private String courseNo;
	private String week;
	private String firstClass;
	private String lastClass;
	private String firstWeek;
	private String lastWeek;
	private String classroom;
	private String teachers;
	private String students;
	private String uuid;
	private String collegeCode;
	private String tNo="";
	
	AdminCourseManageService adminCourseManageService = ServiceFactory.createAdminCourseManageService();
	
	public void index() throws ParseException{

		List<Record> r=adminCourseManageService.lookMajor();
        if(r!=null){
            setAttr("major", r);
        }
		
		List<Record> allTerm=adminCourseManageService.lookTerm();
		setAttr("allTerm", allTerm);

		
		//查找学院
		List<Record> collegesmode=adminCourseManageService.lookCollege();
        if(collegesmode!=null){
        	List<Record> colleges = new ArrayList<Record>();
        	Record none = new Record().set("collegeCode", "全部").set("collegeName", "全部");
			colleges.add(none);
			for(int i = 0;i < collegesmode.size();i++){
				colleges.add(collegesmode.get(i));
			}
        	setAttr("college", colleges);
        }

		//查找课程
		List<Record> courses=adminCourseManageService.lookCourse();
        if(courses!=null){
            setAttr("course", courses);
        }
		
		//查找所有教室
		List<Record> classroom=adminCourseManageService.lookClassroom();
        if(classroom!=null){
            setAttr("classroom", classroom);
            
            List<Record> classroomTypes=Db.find("select DISTINCT classroomType from classroom");
            System.out.println("                                                     "+classroomTypes);
    		setAttr("classroomType",classroomTypes);
        }
					
		//查找所有教师
		List<Record> teacher=adminCourseManageService.lookTeacher();
        if(teacher!=null){
            setAttr("teacher", teacher);
        }
		
				
		//查找所有学生
		List<Record> student=adminCourseManageService.lookStudent();
        if(student!=null){
            setAttr("student", student);	
        }
			
		render("courseManage.html");
	}
	
	public void termManage(){
		System.out.println("学期管理");
		List<Record> allTerm=adminCourseManageService.lookTerm();
		setAttr("allTerm", allTerm);
		render("termManage.html");
	}
	
	public void addTerm() throws ParseException{
		String num=null;
		int beforeTerm;
		int afterTerm;
		String currentTerm=adminCourseManageService.lookCurrentTerm();
		num=currentTerm.substring(10, 11);
		if(num.equals("1")){
			term=currentTerm.substring(0,10)+"2"+")";
		}
		else{
			beforeTerm=Integer.parseInt(currentTerm.substring(0,4))+1;
			afterTerm=Integer.parseInt(currentTerm.substring(0,4))+2;
			term=beforeTerm+""+"/"+afterTerm+""+"(1)";
			System.out.println(term);
		}
		
		int flag=adminCourseManageService.addTerm(term);
		System.out.println("插入学期:"+flag);
		if(flag==0){
			setAttr("message","新学期已存在，新增失败");
		}
		else{
			setAttr("message", "新增成功");
		}
		List<Record> allTerm=adminCourseManageService.lookTerm();
		setAttr("allTerm", allTerm);
		index();
	}
	public void setCurrentTerm() throws ParseException{
		term= getPara("term");
		System.out.println(term);
		int flag=adminCourseManageService.setCurrentTerm(term);
		List<Record> allTerm=adminCourseManageService.lookTerm();
		setAttr("allTerm", allTerm);
		index();
	}
	
	public void setInvalidTerm(){
		term= getPara("term");
		System.out.println(term);
		int flag=adminCourseManageService.setInvalidTerm(term);
		List<Record> allTerm=adminCourseManageService.lookTerm();
		setAttr("allTerm", allTerm);
		render("termManage.html");
	}
	/*public void deleteTerm(){
		term= getPara("term");
		System.out.println(term);
		int flag=adminCourseManageService.deleteTerm(term);
		List<Record> allTerm=adminCourseManageService.lookTerm();
		setAttr("allTerm", allTerm);
		render("termManage.html");
	}*/
	//通过专业查找课程
	public void setMajor2(){
		//查找专业
		List<Record> r=adminCourseManageService.lookMajor();
		setAttr("major", r);
		
		System.out.println("设置专业2");
		majorNo = getPara("majorName");
		System.out.println(majorNo);
		term = getPara("term");
		
		List<Record> list=adminCourseManageService.lookmajorNo(majorNo);	
		majorName=list.get(0).getStr("majorName");
					
		//通过专业和学期查找已经排过的课程
		List<Record> courses=adminCourseManageService.lookmajorTimetable(majorNo,term);
		String majorTimetable=JsonKit.toJson(courses);
		System.out.println(majorTimetable);
		setAttr("majorTimetable",majorTimetable);
		renderJson(majorTimetable);
	}
	
	
	
	public void setMajor() throws ParseException{
		//查找专业
		List<Record> r=adminCourseManageService.lookMajor();
		setAttr("major", r);
		List<Record> allTerm=adminCourseManageService.lookTerm();
		setAttr("allTerm", allTerm);
		System.out.println("设置专业");	
		term = getPara("term");
		
		if(term == null){
            index();
            return;
        }

		setAttr("term", term);
		System.out.println(term);
		//查找学院
		List<Record> collegesmode=adminCourseManageService.lookCollege();
        if(collegesmode!=null){
        	List<Record> colleges = new ArrayList<Record>();
        	Record none = new Record().set("collegeCode", "全部").set("collegeName", "全部");
			colleges.add(none);
			for(int i = 0;i < collegesmode.size();i++){
				colleges.add(collegesmode.get(i));
			}
        	setAttr("college", colleges);
        }


		//查找课程
		List<Record> courses=adminCourseManageService.lookCourse();
        if(courses!=null){
            setAttr("course", courses);
        }
		
		//查找所有教室
		List<Record> classroom=adminCourseManageService.lookClassroom();
        if(classroom!=null){
            setAttr("classroom", classroom);
            
            List<Record> classroomTypes=Db.find("select DISTINCT classroomType from classroom");
            System.out.println(classroomTypes);
    		setAttr("classroomType",classroomTypes);
        }
					
		//查找所有教师
		List<Record> teacher=adminCourseManageService.lookTeacher();
        if(teacher!=null){
            setAttr("teacher", teacher);
        }
		
				
		//查找所有学生
		List<Record> student=adminCourseManageService.lookStudent();
        if(student!=null){
            setAttr("student", student);	
        }
		
		render("courseManage.html");
	}
	
	public void lookCourse(){
		System.out.println("通过学院选择课程");
		collegeCode=getPara("collegeCode");
		System.out.println(collegeCode);
		String strCourses=null;
//		List<Record> courses=adminCourseManageService.lookCourseByCollege(collegeCode);
		List<Record> courses = Db.find("select * from course where collegeCode=?",collegeCode);
		
		System.out.println("----");
		if(courses.size() > 0){
			strCourses=JsonKit.toJson(courses);
			System.out.println(strCourses+"......");
		}
		else{
			
			strCourses=JsonKit.toJson("");	
		}
		renderJson(strCourses);
	}
	
	public void lookTeacher(){
		System.out.println("通过学院选择老师");
		collegeCode=getPara("collegeCode");
		System.out.println(collegeCode);
		String strTeachers=null;
		List<Record> teachers=adminCourseManageService.lookTeacherByCollege(collegeCode);

		System.out.println("----");
		if(teachers!=null){
			strTeachers=JsonKit.toJson(teachers);
			System.out.println(strTeachers+"......");
		}
		else{
			strTeachers=JsonKit.toJson("");	
		}

		renderJson(strTeachers);
	}
	
	public void lookClassroom(){
		System.out.println("通过教室类型选择教室");
		String classroomType=getPara("classroomType");
		System.out.println(classroomType);
		String strClassrooms=null;
		List<Record> classrooms=adminCourseManageService.lookClassroomByType(classroomType);

		System.out.println("----");
		if(classrooms!=null){
			strClassrooms=JsonKit.toJson(classrooms);
			System.out.println(strClassrooms+"......");
		}
		else{
			strClassrooms=JsonKit.toJson("");	
		}

		renderJson(strClassrooms);
	}
	
	//选择助教
	public void lookAssistant(){
		String assistant = getPara("assistant");

		if(assistant != null){
			assistant = JsonKit.toJson(assistant);
		}
		else{
			assistant = JsonKit.toJson("");	
		}
		renderJson(assistant);
	}
	
	public void lookStudent() throws ParseException{
		System.out.println("通过专业和入学年份选择学生");
		String eduStartDate=getPara("startDate");
		String majorNo=getPara("majorNo");
		System.out.println(eduStartDate+majorNo);
		String strStudents=null;
		List<Record> students=adminCourseManageService.lookStudentByDateAndMajorNo(eduStartDate,majorNo);

		System.out.println("----");
		if(students!=null){
			strStudents=JsonKit.toJson(students);
			
		}
		else{
			strStudents=JsonKit.toJson("");		
		}
		System.out.println(strStudents+"......");
		renderJson(strStudents);
	}
	
	//设置课程所需的条件
	public void setCourse() throws ParseException{
		System.out.println("新增课程");
		String[] student=new String[200];
		String[] teacher=new String[200];

		List studentList = new ArrayList();
		List teacherList = new ArrayList();
		List majorList = new ArrayList();
		String classroomJudge = "ok";
		String stuMessage="";
		String stuConflick="";
		String teaMessage="";
		String classroomMessage="";
		
		uuid=UUID.randomUUID().toString();
		majorNo = getPara("majorName");
		term = getPara("term");
		
		courseNo=getPara("courseName");
		week=getPara("week");
		firstClass=getPara("firstClass");
		lastClass=getPara("lastClass");
		firstWeek=getPara("firstWeek");
		lastWeek=getPara("lastWeek");
		
		classroom=getPara("classrooms");
		teachers=getPara("teachers");
		students=getPara("students");
		
		String assistant = getPara("assistant");
		
		System.out.println("==================================" + assistant);
		//System.out.println(courseName+week+firstClass+lastClass+firstWeek+lastWeek+classroom+teachers+students);		

		if(Integer.parseInt(firstClass)>Integer.parseInt(lastClass)){
			message+="第一节课在第二节课之前,新增课程失败!";
		}

		if(Integer.parseInt(firstWeek)>Integer.parseInt(lastWeek)){
			message+="第一周在第二周之前,新增课程失败!";
		}
		if(Integer.parseInt(firstClass)<=Integer.parseInt(lastClass)&&Integer.parseInt(firstWeek)<=Integer.parseInt(lastWeek)){
		
			student = students.split(",");
			teacher = teachers.split(",");
            System.out.println("学生数"+student.length);
            for(int i=0;i<student.length;i++){
                System.out.println("1"+student[i]+"2");
            }
            
			List<Record> list = adminCourseManageService.lookCourseName(courseNo);
			courseName = list.get(0).getStr("courseName");
			List<Record> list2 = adminCourseManageService.lookmajorNo(majorNo);
			majorName = list2.get(0).getStr("majorName");

			// 判断冲突
			for (int x = 0; x < student.length; x++) {
				String stuJudge = adminCourseManageService.studentConflick(student[x], term, week, firstClass,
						lastClass, firstWeek, lastWeek,uuid);
				if (stuJudge.equals("ok")) {
					studentList.add(student[x]);
				} else {
					stuConflick += student[x] + " ";
				}
			}
			// 插入教室
            if(classroom.equals("")){
                classroomJudge="ok";
            }
            else{
               String exist = adminCourseManageService.lookClassroomByOption(classroom);
			// System.out.println(exist);
                if (exist.equals("error")) {
                    classroomJudge = adminCourseManageService.classroomConflick(classroom, week, firstClass, lastClass,
						firstWeek, lastWeek, term,uuid);
                    System.out.println(classroomJudge);
                    if (classroomJudge.equals("ok")) {
                    	int judge3 = adminCourseManageService.updateClassroomStatus(classroom);
                    } else {
                    	classroomMessage = classroom + "教室有冲突," + "新增课程失败";
                    }
                } else {
                    int judge3 = adminCourseManageService.updateClassroomStatus(classroom);
                }
                System.out.println("教室冲突:" + classroomMessage); 
            }
			
			// renderText(classroomMessage);
			if (classroomJudge.equals("ok") && studentList.size() > 0) {
				// 插入教师
                if(teacher.length==1&&teacher[0].equals("")){
                        System.out.println("新增课程无老师");
                        teacherList.add("");
                }
                else{
                    for (int i = 0; i < teacher.length; i++) {
                        Record r = adminCourseManageService.lookTeaId(teacher[i]);
					// System.out.println(teacher[i]);
                        String teaJudge = adminCourseManageService.teaConflick(r.getStr("tNo"), term, week, firstClass,
							lastClass, firstWeek, lastWeek, uuid);
                        System.out.println(teaJudge);
                        if (teaJudge.equals("ok")) {
                            int judge2 = adminCourseManageService.saveTeaTimetable(r.getStr("tNo"), term, courseNo,
								courseName, classroom, firstClass, lastClass, firstWeek, lastWeek, week, uuid);
						// System.out.println(judge2);
                            teacherList.add(teacher[i]);

                        } else {
						teaMessage += teacher[i] + " ";
                        }
                    }
                    if ("".equals(teaMessage)) {
                        
                    } else {
                        teaMessage += "这些教师有冲突";
                    }
                    System.out.println("教师冲突:" + teaMessage);
                    for (int i = 0; i < teacherList.size(); i++) {
                        System.out.println(teacherList.get(i));
                    }
                }
				

				if (teacherList.size() > 0) {
					// 插入学生
                    teachers = "";
                    if(teacher.length==1&&teacher[0].equals("")){
                        System.out.println("新增课程无老师");          
                    }
                    else{
                        for (int i = 0; i < teacherList.size(); i++) {
                            Record r = adminCourseManageService.lookTeaId(teacherList.get(i).toString());
                            tNo += r.getStr("tNo") + ",";
                            teachers += teacherList.get(i) + ",";
                        } 
                    }
					
					for (int i = 0; i < student.length; i++) {
						System.out.println(student[i]);
						String stuJudge = adminCourseManageService.studentConflick(student[i], term, week, firstClass,
								lastClass, firstWeek, lastWeek,uuid);
						System.out.println("学生冲突:"+stuJudge);
						if (stuJudge.equals("ok")) {
							String major = adminCourseManageService.lookStuMajor(student[i]);
                            if(major!=null){
                                majorList.add(major);
                            }							
							int judge1 = adminCourseManageService.saveStuTimetable(student[i], term, courseNo,
									courseName, tNo, teachers, classroom, firstClass, lastClass, firstWeek, lastWeek,
									week, uuid);
							// System.out.println(judge1);
						}
					}
					if ("".equals(stuConflick)) {

					} else {
						stuMessage += stuConflick + "这些学生有冲突";
					}
					// renderText(stuMessage);
					// 给每个老师设置花名册
					System.out.println("花名册");
                    if(teacher.length==1&&teacher[0].equals("")){
                        System.out.println("新增课程无老师,不需要创建花名册");
                    }
                    else{
                        for (int i = 0; i < teacherList.size(); i++) {
                            Record r = adminCourseManageService.lookTeaId(teacherList.get(i).toString());
                            for (int j = 0; j < studentList.size(); j++) {
                                int judge = adminCourseManageService.insertRoster(r.getStr("tNo"), term, courseNo,
                           			courseName, studentList.get(j).toString(), uuid);
                            }
                        } 
                        teachers = teachers.substring(0, teachers.lastIndexOf(","));
                        tNo = tNo.substring(0, tNo.lastIndexOf(","));
                    }				
					// 给该专业该学期插入课程					
					System.out.println(teachers);
                    if(student.length==1&&student[0].equals("")){
                        System.out.println("新增课程无学生"+majorName);
                        majorList.add(majorName);
                    }
                    else{
                        System.out.println("新增课程有学生");
                    }
					for (int i = 0; i < majorList.size(); i++) {
                        System.out.println(majorList.get(i));
						for (int j = i + 1; j < majorList.size(); j++) {
							if (majorList.get(i).toString().equals(majorList.get(j).toString())) {
								majorList.remove(j);
								j--;
							}
						}
					}
					for (int i = 0; i < majorList.size(); i++) {
						System.out.println(majorList.get(i));
						int judge = adminCourseManageService.insertMajorTimetable(term, majorList.get(i).toString(),
								courseName, tNo, teachers, firstClass, lastClass, firstWeek, lastWeek, week, classroom,
								uuid);
					}

				} else {
					teaMessage = "所有教师有冲突,新增课程失败";
				}
			} else {
				stuMessage = "所有学生有冲突,新增课程失败";
			}
			
			if(!"".equals(classroomMessage)){
				message+=classroomMessage+";";
			}
			else{
				if(!"".equals(stuMessage)){
					message+=stuMessage+";";
					if(!"".equals(teaMessage)){
						message+=teaMessage+";";
					}
				}
				else{
					if(!"".equals(teaMessage)){
						message+=teaMessage+";";
					}
					else{
						message+="新增课程成功";
						addAssistant(assistant,uuid);
						adminCourseManageService.courseTest(term,courseNo,tNo,uuid);//新建一门考试
					}
				}
			}
		}
        String majorTimetable=null;
        System.out.println(message);
		List<Record> courses=adminCourseManageService.lookmajorTimetable(majorNo,term);
        if(courses==null){
            Record aa=new Record();
            aa.set("message", message);
            List<Record> nullCourse = new ArrayList<Record>();
            nullCourse.add(aa);
			majorTimetable=JsonKit.toJson(nullCourse);
			System.out.println("新增课时if");
        }
        else{
            courses.get(0).set("message", message);
            majorTimetable=JsonKit.toJson(courses);
            System.out.println(majorTimetable);
            System.out.println(message);
            System.out.println("新增课时else！！！");
        }
		
        
		renderJson(majorTimetable);
	}
	
	public void showUpdateCourse() throws ParseException{
		System.out.println("显示要修改的课表信息");
		String students="";
		String teachers="";
		String stud="";
		String assistant="";
		int stuNumber = 0;
		majorNo = getPara("majorNo");
		term = getPara("term");
		courseNo=getPara("courseNo");
		uuid=getPara("uuid");
		System.out.println(uuid);
		
		List<Record> list=adminCourseManageService.lookCourseName(courseNo);
		courseName=list.get(0).getStr("courseName");
;
		List<Record> stuList=adminCourseManageService.lookCourseStuInfo(uuid);
        if(stuList==null){
            students="";
        }
        else{
            for(int i=0;i<stuList.size();i++){
//			students+=stuList.get(i).getStr("newpassportNo")+",";
            students+=stuList.get(i).getStr("sNo")+",";
            
			stud+=stuList.get(i).getStr("passportNo")+",";
			stuNumber += 1;
            }
            students=students.substring(0,students.lastIndexOf(","));
            stud=stud.substring(0,stud.lastIndexOf(","));
            System.out.println(students);
        }
        
		List<Record> teaList=adminCourseManageService.lookCourseTeaInfo(uuid);
        if(teaList==null){
            teachers="";
        }
        else{
            for(int i=0;i<teaList.size();i++){
			Record r = adminCourseManageService.lookTeaName(teaList.get(i).getStr("tNo"));
			teachers+=r.get("tName")+",";
            }
            teachers=teachers.substring(0,teachers.lastIndexOf(","));
            System.out.println(teachers);
        }
		
        assistant = Db.findFirst("select * from major_timetable where uuid=?",uuid).getStr("assistant_tName");
		String stuTea="[{"+"\""+"stu"+"\""+":"+"\""+students+"\""+","+"\""+"tea"+"\""+":"+"\""+teachers+"\""+","+"\""+"assistant"+"\""+":"+"\""+assistant+"\""+","+"\""+"stuNumber"+"\""+":"+"\""+stuNumber+"\""+","+"\""+"passportNo"+"\""+":"+"\""+stud+"\"}] ";
		System.out.println(stuTea);
		renderJson(stuTea);
	}
	
	//修改课程
	public void updateCourse() throws ParseException{
		List studentList = new ArrayList();
		List teacherList = new ArrayList();
		List majorList = new ArrayList();
		String existCTS=null;
		String classroomJudge = "ok";
		String stuMessage="";
		String teaMessage="";
		String classroomMessage="";
		String preStudent="";
		String preTeacher="";
		int judge1=0;
		int judge2=0;
		int judge3=0;
		int judge4=0;
		String[] student=new String[200];
		String[] teacher=new String[200];
		String[] nowStudent=new String[200];
		String[] nowTeacher=new String[200];
		
		majorNo = getPara("majorName");
		term = getPara("term");
		
		courseNo=getPara("courseNo");
		week=getPara("week");
		firstClass=getPara("firstClass");
		lastClass=getPara("lastClass");
		firstWeek=getPara("firstWeek");
		lastWeek=getPara("lastWeek");
		uuid=getPara("uuid");
		classroom=getPara("classrooms");
		teachers=getPara("teachers");
		students=getPara("students");
		
		System.out.println("==========新传递：" + getPara("temp"));
		String assistant = getPara("assistant");
		System.out.println("===============================" + assistant);
		System.out.println(uuid+courseNo+week+firstClass+lastClass+firstWeek+lastWeek+classroom+teachers+students);
		
		int result1=firstClass.compareTo(lastClass);
		if(Integer.parseInt(firstClass)>Integer.parseInt(lastClass)){
			message+="第一节课在第二节课之前,新增课程失败!";
		}

		if(Integer.parseInt(firstWeek)>Integer.parseInt(lastWeek)){
			message+="第一周在第二周之前,新增课程失败!";
		}
		if(Integer.parseInt(firstClass)<=Integer.parseInt(lastClass)&&Integer.parseInt(firstWeek)<=Integer.parseInt(lastWeek)){
			List<Record> list=adminCourseManageService.lookCourseName(courseNo);
			courseName=list.get(0).getStr("courseName");
			List<Record> list2=adminCourseManageService.lookmajorNo(majorNo);	
			majorName=list2.get(0).getStr("majorName");
			
			student=students.split(",");
			teacher=teachers.split(",");
				
			List<Record> stuList=adminCourseManageService.lookCourseStuByUUID(uuid);
            if(stuList==null){
                preStudent="";
            }
            else{
                for(int i=0;i<stuList.size();i++){
                    preStudent+=stuList.get(i).getStr("passportNo")+",";
                }	
                preStudent=preStudent.substring(0,preStudent.lastIndexOf(","));
            }
					
			List<Record> teaList=adminCourseManageService.lookCourseTeaByUUID(uuid);
            if(teaList==null){
                preTeacher="";
            }
            else{
                for(int i=0;i<teaList.size();i++){
                    Record r = adminCourseManageService.lookTeaName(teaList.get(i).getStr("tNo"));
                    preTeacher+=r.get("tName")+",";
                }
                preTeacher=preTeacher.substring(0,preTeacher.lastIndexOf(","));			 
            }
			
			
			
			System.out.println("初始学生:"+preStudent);
			System.out.println("初始老师:"+preTeacher);
			System.out.println("修改后的学生:"+students+"修改后学生数:"+student.length);
			System.out.println("修改后的老师:"+teachers+"修改后老师数:"+teacher.length);
			
			List<Record> course=adminCourseManageService.lookmajorTimetableByUUID(uuid);	
			if(course.get(0).getStr("classroomNo").equals(classroom)){
				System.out.println("该课程教室没变");
				if(course.get(0).getStr("tName").equals(teachers)){
					System.out.println("该课程老师没变");
					if(preStudent.equals(students)){
						System.out.println("该课程学生没变");
                        if(teachers.equals("")){
                            tNo="";
                        }
                        else{
                            tNo=adminCourseManageService.looktNos(teachers);
                        }
						
						judge1=adminCourseManageService.noRoomNoTeaNoStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,tNo,student,teacher,term);				
						if(judge1==1){
							message+="修改课程成功";
						}
						else{
							message+="存在冲突,不能直接修改课程的时间";
						}
					}
					else{
						System.out.println("该课程学生发生变化");
						if(teachers.equals("")){
                            tNo="";
                        }
                        else{
                            tNo=adminCourseManageService.looktNos(teachers);
                        }
                        existCTS=adminCourseManageService.noRoomNoTeaYesStuExist(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName);
						if(existCTS==null){
							message="修改后的该课程不存在学生,修改课程失败,请重新选择学生";
						}
						else{
							String strJudge=adminCourseManageService.noRoomNoTeaYesStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName,courseNo,tNo,majorName);							
							message=strJudge;
                            if(!teachers.equals("")){
                                judge2=adminCourseManageService.updateTeaTimetable(week,firstClass,lastClass,firstWeek,lastWeek,classroom,uuid);
                               
                                System.out.println(existCTS);
                                //修改花名册
                                for (int i = 0; i < teacher.length; i++) {
                                    System.out.println(teacher[i]);
                                    Record r = adminCourseManageService.lookTeaId(teacher[i]);
                                    adminCourseManageService.deleteRoster(uuid);
                                    nowStudent = existCTS.split(",");
                                    for (int j = 0; j < nowStudent.length; j++) {
                                        System.out.println(j + ":" + nowStudent[j]);
                                        int judge=adminCourseManageService.insertRoster(r.getStr("tNo"),term,courseNo,courseName,nowStudent[j],uuid);					
                                    }
                                }
                            }
                            
                        }
					}
				}
				else{
					System.out.println("该课程老师发生变化");
					//判断该课程修改后还有没有老师
					existCTS=adminCourseManageService.noRoomYesTeaNoStuExist(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,teaList,teacher,term,courseName);
					if(existCTS==null){
						message="修改后的该课程不存在老师,修改课程失败,请重新选择老师";
					}
					else{
						//删除原来老师的花名册
						adminCourseManageService.deleteRoster(uuid);		
						System.out.println("现在的老师"+existCTS);
                        if(existCTS.equals("")){
                            tNo="";
                        }
                        else{
                            tNo=adminCourseManageService.looktNos(existCTS);
                        }
						
						nowTeacher = existCTS.split(",");
						if(preStudent.equals(students)){
							System.out.println("该课程学生没变");
 							String teaJudge=adminCourseManageService.noRoomYesTeaNoStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,teaList,teacher,term,courseName,courseNo,tNo);			
							message=teaJudge;
                            if(!teachers.equals("")){
                                for (int i = 0; i < nowTeacher.length; i++) {
								System.out.println(nowTeacher[i]);
								Record r = adminCourseManageService.lookTeaId(nowTeacher[i]);
                                if(stuList!=null){
                                   for (int j = 0; j < stuList.size(); j++) {
									System.out.println(j + ":" + stuList.get(j).getStr("passportNo"));
									int judge=adminCourseManageService.insertRoster(r.getStr("tNo"),term,courseNo,courseName,stuList.get(j).getStr("passportNo"),uuid);					
								} 
                                }
								
							}
                            }
							
						}
						else{
							System.out.println("该课程学生发生变化");
							//判断该课程修改后还有没有学生
							existCTS=adminCourseManageService.noRoomNoTeaYesStuExist(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName);
							if(existCTS==null){
								message="修改后的该课程不存在学生,修改课程失败,请重新选择学生";
							}
							else{
								String teaJudge=adminCourseManageService.noRoomYesTeaYesStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName,teaList,teacher,courseNo,tNo,majorName);							
								message=teaJudge;
								System.out.println(existCTS);
								//修改花名册
                                if(!teachers.equals("")){
								for (int i = 0; i < nowTeacher.length; i++) {
									System.out.println(nowTeacher[i]);
									Record r = adminCourseManageService.lookTeaId(nowTeacher[i]);
									nowStudent = existCTS.split(",");
									for (int j = 0; j < nowStudent.length; j++) {
										System.out.println(j + ":" + nowStudent[j]);
										int judge=adminCourseManageService.insertRoster(r.getStr("tNo"),term,courseNo,courseName,nowStudent[j],uuid);					
									}
								}
                                }
							}
							
						}
					}
					
				}
				
			}
			else{
				System.out.println("该课程教室发生变化");
                if(!classroom.equals("")){
                    String exist = adminCourseManageService.lookClassroomByOption(classroom);
				//System.out.println(exist);
                    if (exist.equals("error")) {
                        classroomJudge = adminCourseManageService.classroomConflick(classroom, week, firstClass, lastClass,
							firstWeek, lastWeek,term,uuid);
                        System.out.println(classroomJudge);
                        if (classroomJudge.equals("ok")) {
                            judge3 = adminCourseManageService.updateClassroomStatus(classroom);
                        } else {
                            classroomMessage = classroom + "教室有冲突";
                        }
                    } else {
                        judge3 = adminCourseManageService.updateClassroomStatus(classroom);
                    }
                }
                else{
                    classroomJudge="ok";
                }
				
				if(classroomJudge.equals("ok")){
					if(course.get(0).getStr("tName").equals(teachers)){
						System.out.println("该课程老师没变");
						if(preStudent.equals(students)){	
							System.out.println("该课程学生没变");
                            if(teachers.equals("")){
                                tNo="";
                            }
                            else{
                                tNo=adminCourseManageService.looktNos(teachers);
                            }
							judge1=adminCourseManageService.yesRoomNoTeaNoStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,tNo,student,teacher,term);			
							if(judge1==1){
								message+="修改课程成功";
							}
							else{
								message+="存在冲突,修改课程失败";
							}
						}
						else{
							System.out.println("该课程学生发生变化");
							if(teachers.equals("")){
                                tNo="";
                            }
                            else{
                                tNo=adminCourseManageService.looktNos(teachers);
                            }
							//判断该课程修改后还有没有学生
							existCTS=adminCourseManageService.noRoomNoTeaYesStuExist(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName);
							if(existCTS==null){
								message="修改后的该课程不存在学生,修改课程失败,请重新选择学生";
							}
							else{
								String strJudge=adminCourseManageService.noRoomNoTeaYesStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName,courseNo,tNo,majorName);							
								judge2=adminCourseManageService.updateTeaTimetable(week,firstClass,lastClass,firstWeek,lastWeek,classroom,uuid);
								//judge3=adminCourseManageService.updateMajorTimetable(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,tNo);
								message=strJudge;
								System.out.println(existCTS);
								//修改花名册
								adminCourseManageService.deleteRoster(uuid);
                                if(!teachers.equals("")){
                                    for (int i = 0; i < teacher.length; i++) {
                                        System.out.println(teacher[i]);
                                        Record r = adminCourseManageService.lookTeaId(teacher[i]);								
                                        nowStudent = existCTS.split(",");
                                        for (int j = 0; j < nowStudent.length; j++) {
                                            System.out.println(j + ":" + nowStudent[j]);
                                            int judge=adminCourseManageService.insertRoster(r.getStr("tNo"),term,courseNo,courseName,nowStudent[j],uuid);					
                                        }
                                    }
                                }
							}
							
							
						}
					}
					else{
						System.out.println("该课程老师发生变化");
						//判断该课程修改后还有没有老师
						existCTS=adminCourseManageService.noRoomYesTeaNoStuExist(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,teaList,teacher,term,courseName);
						if(existCTS==null){
							message="修改后的该课程不存在老师,修改课程失败,请重新选择老师";
						}
						else{
							//删除原来老师的花名册
							adminCourseManageService.deleteRoster(uuid);
							System.out.println("现在的老师"+existCTS);
							if(teachers.equals("")){
                                tNo="";
                            }
                            else{
                                tNo=adminCourseManageService.looktNos(existCTS);
                            }

							nowTeacher = existCTS.split(",");
							if(preStudent.equals(students)){
								System.out.println("该课程学生没变");
								String teaJudge=adminCourseManageService.noRoomYesTeaNoStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,teaList,teacher,term,courseName,courseNo,tNo);			
								message=teaJudge;
                                if(!teachers.equals("")&&!students.equals("")){
								for (int i = 0; i < nowTeacher.length; i++) {
									System.out.println(nowTeacher[i]);
									Record r = adminCourseManageService.lookTeaId(nowTeacher[i]);
									for (int j = 0; j < stuList.size(); j++) {
										System.out.println(j + ":" + stuList.get(j).getStr("passportNo"));
										int judge=adminCourseManageService.insertRoster(r.getStr("tNo"),term,courseNo,courseName,stuList.get(j).getStr("passportNo"),uuid);					
									}
								}
                                }
							}
							else{
								System.out.println("该课程学生发生变化");
								//判断该课程修改后还有没有学生
								existCTS=adminCourseManageService.noRoomNoTeaYesStuExist(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName);
								if(existCTS==null){
									message="修改后的该课程不存在学生,修改课程失败,请重新选择学生";
								}
								else{
									String teaJudge=adminCourseManageService.noRoomYesTeaYesStu(week,firstClass,lastClass,firstWeek,lastWeek,teachers,classroom,uuid,stuList,student,term,courseName,teaList,teacher,courseNo,tNo,majorName);							
									message=teaJudge;
									System.out.println(existCTS);
									//修改花名册
                                    if(!teachers.equals("")){
									for (int i = 0; i < nowTeacher.length; i++) {
										System.out.println(nowTeacher[i]);
										Record r = adminCourseManageService.lookTeaId(nowTeacher[i]);
										nowStudent = existCTS.split(",");
										for (int j = 0; j < nowStudent.length; j++) {
											System.out.println(j + ":" + nowStudent[j]);
											int judge=adminCourseManageService.insertRoster(r.getStr("tNo"),term,courseNo,courseName,nowStudent[j],uuid);					
										}
									}
                                    }
								}
								
							}
							
						}
					}
				}
				else{
					message+="教室冲突,修改失败";
				}
			}
		}

		if("".equals(message)){
			message="修改课程成功";
		}
		System.out.println(message);
		List<Record> courses=adminCourseManageService.lookmajorTimetable(majorNo,term);
		String majorTimetable=null;
		if(courses==null){
            Record aa=new Record();
            aa.set("message", message);
            List<Record> nullCourse = new ArrayList<Record>();
            nullCourse.add(aa);
			majorTimetable=JsonKit.toJson(nullCourse);
			System.out.println("修改课时if");
			System.out.println("修改完該专业没有课程");
		}
		else{
			courses.get(0).set("message", message);
			majorTimetable=JsonKit.toJson(courses);
			System.out.println(majorTimetable);
			System.out.println("修改课时else");
		}
		
		//修改考试表中的信息
		adminCourseManageService.updataTest(uuid);
		addAssistant(assistant,uuid);
		renderJson(majorTimetable);

	}
	
	//删除课程
	public void deleteCourse() throws ParseException{
		String tName;
		majorNo = getPara("majorName");
		term = getPara("term");
		
		/*eduStartDate = getPara("eduStartDate");
		eduStartSeason = getPara("eduStartSeason");
		System.out.println(majorName+term+eduStartDate+eduStartSeason);	*/
		
		courseNo=getPara("courseNo");
		tName=getPara("tName");
		uuid=getPara("uuid");
		System.out.println(majorNo+term+courseNo+tName+uuid);
		String[] student=new String[200];
		String[] teacher=new String[200];
		String majorTimetable=null;
		List<Record> list=adminCourseManageService.lookCourseName(courseNo);
		courseName=list.get(0).getStr("courseName");
		List<Record> list2=adminCourseManageService.lookmajorNo(majorNo);	
		majorName=list2.get(0).getStr("majorName");
		System.out.println(courseName+majorName);

		int Grade = adminCourseManageService.lookStuGrade(uuid);
		if(Grade==1){
			System.out.println("该课程没有成绩,可以删除");
			teacher=tName.split(",");
			System.out.println(teacher);
			//删除该课程教师
			int judge1 = adminCourseManageService.deleteTeacherTimetable(uuid);
			System.out.println("删除教师:"+judge1);

			
			//删除该课程学生
			int judge2 = adminCourseManageService.deleteStudentTimetable(uuid);
			
			//删除该课程花名册

			adminCourseManageService.deleteRoster(uuid);		

			//删除专业课表
			adminCourseManageService.deleteMajorTimetable(uuid);
			List<Record> courses=adminCourseManageService.lookmajorTimetable(majorNo,term);
			majorTimetable=JsonKit.toJson(courses);
			
			//删除考试
			adminCourseManageService.deleteTest(uuid);
		}
		else{
			System.out.println("该课程已有成绩,不可删除");
			List<Record> courses=adminCourseManageService.lookmajorTimetable(majorNo,term);
			courses.get(0).set("message", "该课程已有成绩,不可删除");
			majorTimetable=JsonKit.toJson(courses);
		}
		renderJson(majorTimetable);
	}
	
	
	//新增课时-----杨昆鹏
	public void addCourse() throws ParseException{
		System.out.println("-------------------1111----");

		System.out.println("新增课时");
		String[] student=new String[200];
		String[] teacher=new String[200];

		List studentList = new ArrayList();
		List teacherList = new ArrayList();
		List majorList = new ArrayList();
		String classroomJudge = "ok";
		String stuMessage="";
		String stuConflick="";
		String teaMessage="";
		String classroomMessage="";
		
//		uuid=UUID.randomUUID().toString();
		uuid = getPara("uuid");
		majorNo = getPara("majorName");
		term = getPara("term");
		
		System.out.println(uuid+"==="+majorNo+"==="+term);
		
		courseNo=getPara("courseName");
		week=getPara("week");
		firstClass=getPara("firstClass");
		lastClass=getPara("lastClass");
		firstWeek=getPara("firstWeek");
		lastWeek=getPara("lastWeek");
		
		System.out.println(courseNo+"==="+firstClass+lastClass+"==="+firstWeek+lastWeek+"==="+term);
		
		classroom=getPara("classrooms");
		teachers=getPara("teachers");
		students=getPara("students");
		
		String assistant = getPara("assistant");
		
		System.out.println("==============================="+assistant);
		//System.out.println(courseName+week+firstClass+lastClass+firstWeek+lastWeek+classroom+teachers+students);		
		System.out.println(classroom+"==="+teachers+"==="+students);
		
		if(Integer.parseInt(firstClass)>Integer.parseInt(lastClass)){
			message+="第一节课在第二节课之前,新增课程失败!";
		}

		if(Integer.parseInt(firstWeek)>Integer.parseInt(lastWeek)){
			message+="第一周在第二周之前,新增课程失败!";
		}
		if(Integer.parseInt(firstClass)<=Integer.parseInt(lastClass)&&Integer.parseInt(firstWeek)<=Integer.parseInt(lastWeek)){
		
			student = students.split(",");
			teacher = teachers.split(",");
            System.out.println("学生数"+student.length);
            for(int i=0;i<student.length;i++){
                System.out.println("1"+student[i]+"2");
            }
			List<Record> list = adminCourseManageService.lookCourseName(courseNo);
			courseName = list.get(0).getStr("courseName").toString();
			List<Record> list2 = adminCourseManageService.lookmajorNo(majorNo);
			majorName = list2.get(0).getStr("majorName").toString();

			// 判断冲突
			for (int x = 0; x < student.length; x++) {
				String stuJudge = adminCourseManageService.studentConflick(student[x], term, week, firstClass,
						lastClass, firstWeek, lastWeek,uuid);
				if (stuJudge.equals("ok")) {
					studentList.add(student[x]);
				} else {
					stuConflick += student[x] + " ";
				}
			}
			// 插入教室
            if(classroom.equals("")){
                classroomJudge="ok";
            }
            else{
               String exist = adminCourseManageService.lookClassroomByOption(classroom);
			// System.out.println(exist);
                if (exist.equals("error")) {
                    classroomJudge = adminCourseManageService.classroomConflick(classroom, week, firstClass, lastClass,
						firstWeek, lastWeek, term,uuid);
                    System.out.println(classroomJudge);
                    if (classroomJudge.equals("ok")) {
                    	int judge3 = adminCourseManageService.updateClassroomStatus(classroom);
                    } else {
                    	classroomMessage = classroom + "教室有冲突," + "新增课程失败";
                    }
                } else {
                    int judge3 = adminCourseManageService.updateClassroomStatus(classroom);//研读
                }
                System.out.println("教室冲突:" + classroomMessage); 
            }
			
			// renderText(classroomMessage);
			if (classroomJudge.equals("ok") && studentList.size() > 0) {
				// 插入教师
                if(teacher.length==1&&teacher[0].equals("")){
                        System.out.println("新增课程无老师");
                        teacherList.add("");
                }
                else{
                    for (int i = 0; i < teacher.length; i++) {
                        Record r = adminCourseManageService.lookTeaId(teacher[i]);
					// System.out.println(teacher[i]);
                        String teaJudge = adminCourseManageService.teaConflick(r.getStr("tNo"), term, week, firstClass,
							lastClass, firstWeek, lastWeek, uuid);
                        System.out.println(teaJudge);
                        if (teaJudge.equals("ok")) {
                            int judge2 = adminCourseManageService.saveTeaTimetable(r.getStr("tNo"), term, courseNo, //插入表teacher_timetable
								courseName, classroom, firstClass, lastClass, firstWeek, lastWeek, week, uuid);
						// System.out.println(judge2);
                            teacherList.add(teacher[i]);

                        } else {
						teaMessage += teacher[i] + " ";
                        }
                    }
                    if ("".equals(teaMessage)) {
                        
                    } else {
                        teaMessage += "这些教师有冲突";
                    }
                    System.out.println("教师冲突:" + teaMessage);
                    for (int i = 0; i < teacherList.size(); i++) {
                        System.out.println(teacherList.get(i));
                    }
                }
				

				if (teacherList.size() > 0) {
					// 插入学生
                    teachers = "";
                    if(teacher.length==1&&teacher[0].equals("")){
                        System.out.println("新增课程无老师");          
                    }
                    else{
                        for (int i = 0; i < teacherList.size(); i++) {
                            Record r = adminCourseManageService.lookTeaId(teacherList.get(i).toString());
                            tNo += r.getStr("tNo") + ",";
                            teachers += teacherList.get(i) + ",";
                        } 
                    }
					
					for (int i = 0; i < student.length; i++) {
						System.out.println(student[i]);
						String stuJudge = adminCourseManageService.studentConflick(student[i], term, week, firstClass,
								lastClass, firstWeek, lastWeek,uuid);
						System.out.println("学生冲突:"+stuJudge);
						if (stuJudge.equals("ok")) {
							String major = adminCourseManageService.lookStuMajor(student[i]);
                            if(major!=null){
                                majorList.add(major);
                            }							
							int judge1 = 1;
//									adminCourseManageService.saveStuTimetable(student[i], term, courseNo,//插入表student_timetable    X
//									courseName, tNo, teachers, classroom, firstClass, lastClass, firstWeek, lastWeek,
//									week, uuid);
							// System.out.println(judge1);
						}
					}
					if ("".equals(stuConflick)) {

					} else {
						stuMessage += stuConflick + "这些学生有冲突";
					}
					// renderText(stuMessage);
					// 给每个老师设置花名册
					System.out.println("花名册");
                    if(teacher.length==1&&teacher[0].equals("")){
                        System.out.println("新增课程无老师,不需要创建花名册");
                    }
                    else{
                        for (int i = 0; i < teacherList.size(); i++) {
                            Record r = adminCourseManageService.lookTeaId(teacherList.get(i).toString());
                            for (int j = 0; j < studentList.size(); j++) {
                                int judge = 1;
//                                		adminCourseManageService.insertRoster(r.getStr("tNo"), term, courseNo,//插入表roster          X
//                           			courseName, studentList.get(j).toString(), uuid);
                            }
                        } 
                        teachers = teachers.substring(0, teachers.lastIndexOf(","));
                        tNo = tNo.substring(0, tNo.lastIndexOf(","));
                    }				
					// 给该专业该学期插入课程					
					System.out.println(teachers);
                    if(student.length==1&&student[0].equals("")){
                        System.out.println("新增课程无学生"+majorName);
                        majorList.add(majorName);
                    }
                    else{
                        System.out.println("新增课程有学生");
                    }
					for (int i = 0; i < majorList.size(); i++) {
                        System.out.println(majorList.get(i));
						for (int j = i + 1; j < majorList.size(); j++) {
							if (majorList.get(i).toString().equals(majorList.get(j).toString())) {
								majorList.remove(j);
								j--;
							}
						}
					}
					for (int i = 0; i < majorList.size(); i++) {
						System.out.println(majorList.get(i));
						int judge = adminCourseManageService.insertMajorTimetable(term, majorList.get(i).toString(),//插入表major_timetable
								courseName, tNo, teachers, firstClass, lastClass, firstWeek, lastWeek, week, classroom,
								uuid);
						addAssistant(assistant,uuid);
					}

				} else {
					teaMessage = "所有教师有冲突,新增课程失败";
				}
			} else {
				stuMessage = "所有学生有冲突,新增课程失败";
			}
			
			if(!"".equals(classroomMessage)){
				message+=classroomMessage+";";
			}
			else{
				if(!"".equals(stuMessage)){
					message+=stuMessage+";";
					if(!"".equals(teaMessage)){
						message+=teaMessage+";";
					}
				}
				else{
					if(!"".equals(teaMessage)){
						message+=teaMessage+";";
					}
					else{
						message+="新增课程成功";
					}
				}
			}
		}
        String majorTimetable=null;
        System.out.println(message);
		List<Record> courses=adminCourseManageService.lookmajorTimetable(majorNo,term);
        if(courses==null){
            Record aa=new Record();
            aa.set("message", message);
            List<Record> nullCourse = new ArrayList<Record>();
            nullCourse.add(aa);
			majorTimetable=JsonKit.toJson(nullCourse);
        }
        else{
            courses.get(0).set("message", message);
            majorTimetable=JsonKit.toJson(courses);
            System.out.println(majorTimetable);
            System.out.println(message);
        }
		renderJson(majorTimetable);
	
	}
	
	//增加助教
	public void addAssistant(String assistant,String uuid){
		Db.update("update major_timetable set assistant_tName=? where uuid=?",assistant,uuid);
	}
	
	
}

